//
//  CallToServiceViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/30.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "CallToServiceViewController.h"
#import "ServiceCenterTableViewCell.h"
#import "CommonProblemViewController.h"
#import "XHChatQQ.h"

@interface CallToServiceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)UIView * hiddenView;

@property (nonatomic, weak)UIImageView * topImageView;

@property (nonatomic, weak)UILabel * showLabel;

@property (nonatomic, weak)UILabel * numberLabel;

@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, weak)UILabel * backLabel;

@property (nonatomic, weak)UIView * lineView2;

@property (nonatomic, weak)UILabel * sureLabel;

@end

@implementation CallToServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    self.title = @"客服中心";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 * SCREENH6, SCREEN_WIDTH, 195 * SCREENH6)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[ServiceCenterTableViewCell class] forCellReuseIdentifier:@"serviceCenterCell"];
    
    [self.view addSubview:self.tableView];
    
    
    self.hiddenView = [UIView new];
    
    self.hiddenView.hidden = YES;
    self.hiddenView.layer.cornerRadius = 15 * SCREENH6;
    self.hiddenView.backgroundColor = ColorWithRGB(0xe8e8e8);
    self.hiddenView.userInteractionEnabled = YES;
    
    self.hiddenView.layer.masksToBounds = YES;
    
    [self.view addSubview:self.hiddenView];
    
    [self.hiddenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.width.mas_equalTo(273 * SCREENH6);
        make.height.mas_equalTo(163 * SCREENH6);
    }];
    
    UIImageView * topImageView = [[UIImageView alloc] init];
    
    //    132*115
    topImageView.image = [UIImage imageNamed:@"客服电话"];
    
    self.topImageView = topImageView;
    self.topImageView.hidden = YES;
    
    [self.view addSubview:self.topImageView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"DCM客服电话";
    showLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    showLabel.textColor = ColorWithRGB(0x333333);
    
    self.showLabel = showLabel;
    
    [self.hiddenView addSubview:self.showLabel];
    
    
    UILabel * numberLabel = [UILabel new];
    
    numberLabel.text = @"400-670-0002";
    numberLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    numberLabel.textColor = ColorWithRGB(0x333333);
    
    self.numberLabel = numberLabel;
    
    [self.hiddenView addSubview:self.numberLabel];
    
    UIView * lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = ColorWithRGB(0xbebebe);
    
    self.lineView = lineView;
    
    [self.hiddenView addSubview:self.lineView];
    
    
    UILabel * backLabel = [UILabel new];
    
    backLabel.text = @"取消";
    backLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    backLabel.textColor = ColorWithRGB(0x979797);
    backLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapBack = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCancelAction)];
    [backLabel addGestureRecognizer:tapBack];
    
    self.backLabel = backLabel;
    
    [self.hiddenView addSubview:self.backLabel];
    
    UIView * lineView2 = [[UIView alloc] init];
    
    lineView2.backgroundColor = ColorWithRGB(0xbebebe);
    
    self.lineView2 = lineView2;
    
    [self.hiddenView addSubview:self.lineView2];
    
    
    UILabel * sureLabel = [UILabel new];
    
    sureLabel.text = @"拨打";
    sureLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    sureLabel.textColor = ColorWithRGB(0xed4444);
    sureLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapSure = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCallAction)];
    [sureLabel addGestureRecognizer:tapSure];
    
    self.sureLabel = sureLabel;
    
    [self.hiddenView addSubview:self.sureLabel];
    
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.hiddenView);
        make.height.mas_equalTo(59 * SCREENH6);
        make.width.mas_equalTo(91 * SCREENH6);
        make.top.mas_equalTo(self.hiddenView.mas_top).offset(-10 * SCREENH6);
    }];
    
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topImageView.mas_bottom).offset(9 * SCREENH6);
        make.centerX.mas_equalTo(self.hiddenView);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.showLabel.mas_bottom).offset(2 * SCREENH6);
        make.centerX.mas_equalTo(self.hiddenView);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.hiddenView);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.numberLabel.mas_bottom).offset(15 * SCREENH6);
    }];
    
    
    [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.hiddenView).offset(55 * SCREENH6);//50
        make.bottom.mas_equalTo(self.hiddenView).offset(-12 * SCREENH6);//20
    }];
    
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.bottom.mas_equalTo(self.hiddenView);
        make.width.mas_equalTo(1);
        make.centerX.mas_equalTo(self.hiddenView);
    }];
    
    [self.sureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backLabel);
        make.trailing.mas_equalTo(self.hiddenView.mas_trailing).offset(-55 * SCREENH6);
    }];

}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ServiceCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serviceCenterCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.titleLabel.text = @"常见问题";
        cell.subTltleLabel.text = @"";
    }else if (indexPath.row == 1){
        
        cell.titleLabel.text = @"客服电话";
        cell.subTltleLabel.text = @"400-670-0002";
    }else{
        
        cell.titleLabel.text = @"在线客服";
        cell.subTltleLabel.text = @"DCM";
    }
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return 65 * SCREENH6;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.hidesBottomBarWhenPushed = YES;
    
    if (indexPath.row == 0) {
        
        CommonProblemViewController * commonVC = [CommonProblemViewController new];
        
        [self.navigationController pushViewController:commonVC animated:YES];
    }else if (indexPath.row == 1){
        
        self.topImageView.hidden = NO;
        self.hiddenView.hidden = NO;
    }else{
        
//        OnlineServiceViewController * onlineVC = [OnlineServiceViewController new];
//        
//        [self.navigationController pushViewController:onlineVC animated:YES];
        
//        RCConversationViewController *conversationVC = [[RCConversationViewController alloc]initWithConversationType:ConversationType_CUSTOMERSERVICE targetId:@"test"];
//
//        conversationVC.title = @"DCM客服";
//        [self.navigationController pushViewController:conversationVC animated:YES];
        if([XHChatQQ haveQQ])//是否有安装QQ客户端
        {
            
            //2.此处传入的QQ号,需开通QQ推广功能,不然"陌生人"无法向此QQ号发送临时消,(发送时会直接失败).
            //开通QQ推广方法:1.打开QQ推广网址http://shang.qq.com并用QQ登录  2.点击顶部导航栏:推广工具  3.在弹出菜单中点击'立即免费开通' 即可
            
            [XHChatQQ chatWithQQ:@"938005880"];//发起QQ临时会话
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备尚未安装QQ客户端,不能进行QQ临时会话" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
}



- (void)tapCancelAction{
    
    self.topImageView.hidden = YES;
    self.hiddenView.hidden = YES;
}


- (void)tapCallAction{
    
    UIWebView * callWebview = [[UIWebView alloc]init];
    
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:4006700002"]]];
    
    [[UIApplication sharedApplication].keyWindow addSubview:callWebview];

//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];
    
    //http://blog.csdn.net/yuqingzhude/article/details/52117978
    
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
